import scrapy
from mySpider.items import MyspiderItem

class ItcastSpider(scrapy.Spider):
    name = 'itcast'
    allowed_domains = ['itcast.cn']
    start_urls = ['http://www.itcast.cn/channel/teacher.shtml']

    def parse(self, response):
        # with open("teacher_info.txt", "w", encoding="utf-8") as file:
        #     file.write(response.text)
        items = []  # 存储所有讲师的信息
        for each in response.xpath("//div[@class='li_txt']"):
            # 创建MyspiderItem类的对象
            item = MyspiderItem()
            # 使用XPath的路径表达式选取节点
            name = each.xpath("h3/text()").extract()
            level = each.xpath("h4/text()").extract()
            resume = each.xpath("p/text()").extract()
            # 将每个讲师的信息封装成MyspiderItem类的对象
            item["name"] = name[0]
            item["level"] = level[0]
            item["resume"] = resume[0]
            items.append(item)
        # 直接返回数据，而不交给管道组件
        return items

